﻿using Core;
using Core.Caching;
using Core.Data;
using System.Linq;
using Core.Domain.Companys;
using System;
using System.Collections.Generic;

namespace Services.Companys
{
    /// <summary>
    /// 职位服务器
    /// </summary>
    public partial class JobService : IJobService
    {

        #region Constants
        private const string JOB_PATTERN_KEY = "pattern.job.";
        private const string JOB_ID_KEY = "pattern.job.id-{0}";

        #endregion

        #region Fields

        private readonly IRepository<Jobs> _jobRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IRepository<Company> _companyRepository;

        #endregion
        
        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public JobService(
            IRepository<Jobs> jobRepository,
            IRepository<Company> companyRepository,
            ICacheManager cacheManager
            )
        {
            this._jobRepository = jobRepository;
            this._companyRepository = companyRepository;
            this._cacheManager = cacheManager;
        }

        #endregion

        #region Method
        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="entity"></param>
        public void DeleteJob(Jobs entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Jobs");

            entity.Deleted = true;

            _jobRepository.Update(entity);
            _cacheManager.List_Remove(JOB_PATTERN_KEY, entity);
        }

        /// <summary>
        /// 新增职位
        /// </summary>
        /// <param name="entity"></param>
        public void InsertJob(Jobs entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Jobs");

            _jobRepository.Insert(entity);

            _cacheManager.List_Remove(JOB_PATTERN_KEY, entity);
        }

        /// <summary>
        /// 更新职位
        /// </summary>
        /// <param name="entity"></param>
        public void UpdateJob(Jobs entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Jobs");
            
            _jobRepository.Update(entity);
            _cacheManager.List_Remove(JOB_PATTERN_KEY, entity);
        }

        /// <summary>
        /// 根据主键获取职位
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public Jobs GetJobById(int jobId)
        {
            if (jobId == 0)
                return null;

            string key = string.Format(JOB_ID_KEY, jobId);

            return _cacheManager.Get(key, () => { return _jobRepository.GetById(jobId); });
        }

        /// <summary>
        /// 根据主键获取职位集合
        /// </summary>
        /// <param name="jobIds"></param>
        /// <returns></returns>
        public IList<Jobs> GetJobByIds(int[] jobIds)
        {
            if (jobIds == null || jobIds.Length == 0)
                return new List<Jobs>();

            var query = from c in _jobRepository.Table
                        where jobIds.Contains(c.Id)
                        select c;
            return query.ToList();
        }

        /// <summary>
        /// 获取职位
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="active"></param>
        /// <param name="deleted"></param>
        /// <returns></returns>
        public IList<Jobs> GetJobs(int companyId = 0,bool? active = true, bool? deleted = false)
        {
            var query = _jobRepository.Table;
            if (companyId > 0)
                query = query.Where(c => c.CompanyId == companyId);

            if(active.HasValue)
                query = query.Where(c => c.Active == active);

            if (deleted.HasValue)
                query = query.Where(c => c.Deleted == deleted);

            return query.ToList();
        }

        /// <summary>
        /// 分页获取职位
        /// </summary>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="companyId"></param>
        /// <param name="scaleId"></param>
        /// <param name="cityId"></param>
        /// <param name="active"></param>
        /// <param name="deleted"></param>
        /// <param name="values"></param>
        /// <param name="sort"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<Jobs> GetJobs(
            string name = "", string url = "", int companyId = 0,
            int scaleId = 0, int cityId = 0, 
            bool? active = true, bool? deleted = false, 
            List<int> values = null,
            JobSortingEnum sort = JobSortingEnum.CreateUp, 
            int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _jobRepository.Table;
            if (!String.IsNullOrWhiteSpace(name))
                query = query.Where(c => c.Name.Contains(name) || c.Memo.Contains(name));

            if (cityId > 0)
                query = query.Join(_companyRepository.Table, x => x.CompanyId, y => y.Id, (x, y) => new { Jobs = x, Company = y })
                    .Where(z => z.Company.Id == companyId).Select(z => z.Jobs);

            if (companyId > 0)
                query = query.Where(c => c.CompanyId == companyId);

            if (active.HasValue)
                query = query.Where(c => c.Active == active);

            if (deleted.HasValue)
                query = query.Where(c => c.Deleted == deleted);

            switch (sort)
            {
                case JobSortingEnum.CreateUp:
                    query = query.OrderBy(c => c.CreatedOn);
                    break;

                case JobSortingEnum.CreateDown:
                    query = query.OrderByDescending(c => c.CreatedOn);
                    break;
                default:
                    query = query.OrderBy(c => c.CreatedOn);
                    break;
            }

            return new PagedList<Jobs>(query, pageIndex, pageSize);

        }
        #endregion
    }
}
